{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1> Reading NEXRAD Level II data from Google Cloud public datasets </h1>\n",
    "\n",
    "This notebook demonstrates how to use PyART to visualize data from the Google Cloud public dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%bash\n",
    "rm -rf data\n",
    "mkdir data\n",
    "cd data\n",
    "RADAR=KIWA\n",
    "YEAR=2013\n",
    "MONTH=07\n",
    "DAY=23\n",
    "HOUR=23\n",
    "gcloud storage cp gs://gcp-public-data-nexrad-l2/$YEAR/$MONTH/$DAY/$RADAR/*_$RADAR_${YEAR}${MONTH}${DAY}${HOUR}0000_${YEAR}${MONTH}${DAY}${HOUR}5959.tar temp.tar\n",
    "tar xvf temp.tar\n",
    "rm *.tar\n",
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Install Py-ART </h3>\n",
    "\n",
    "See https://github.com/ARM-DOE/pyart/wiki/Simple-Install-of-Py-ART-using-Anaconda"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Plot volume scans using Py-ART within Jupyter </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Based on\n",
    "# http://arm-doe.github.io/pyart/dev/auto_examples/plotting/plot_nexrad_multiple_moments.html\n",
    "# by Jonathan J. Helmus (jhelmus@anl.gov)\n",
    "import matplotlib.pyplot as plt\n",
    "import pyart\n",
    "def plot_data(filename):\n",
    "  radar = pyart.io.read_nexrad_archive(infilename)\n",
    "  display = pyart.graph.RadarDisplay(radar)\n",
    "  fig = plt.figure(figsize=(10, 10))\n",
    "\n",
    "  ax = fig.add_subplot(221)\n",
    "  display.plot('velocity', 1, ax=ax, title='Doppler Velocity',\n",
    "             colorbar_label='',\n",
    "             axislabels=('', 'North South distance from radar (km)'))\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "  ax = fig.add_subplot(222)\n",
    "  display.plot('reflectivity', 0, ax=ax,\n",
    "             title='Reflectivity lowest', colorbar_label='',\n",
    "             axislabels=('', ''))\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "\n",
    "  ax = fig.add_subplot(223)\n",
    "  display.plot('reflectivity', 1, ax=ax,\n",
    "             title='Reflectivity second', colorbar_label='')\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "\n",
    "  ax = fig.add_subplot(224)\n",
    "  display.plot('cross_correlation_ratio', 0, ax=ax,\n",
    "             title='Correlation Coefficient', colorbar_label='',\n",
    "             axislabels=('East West distance from radar (km)', ''))\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "\n",
    "  plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Plot into png </h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting plot_pngs.py\n"
     ]
    }
   ],
   "source": [
    "%writefile plot_pngs.py\n",
    "import matplotlib.pyplot as plt\n",
    "import pyart\n",
    "\n",
    "def plot_data(infilename, outpng):\n",
    "  radar = pyart.io.read_nexrad_archive(infilename)\n",
    "  display = pyart.graph.RadarDisplay(radar)\n",
    "  fig = plt.figure(figsize=(10, 10))\n",
    "\n",
    "  ax = fig.add_subplot(221)\n",
    "  display.plot('velocity', 1, ax=ax, title='Doppler Velocity',\n",
    "             colorbar_label='',\n",
    "             axislabels=('', 'North South distance from radar (km)'))\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "  ax = fig.add_subplot(222)\n",
    "  display.plot('reflectivity', 0, ax=ax,\n",
    "             title='Reflectivity lowest', colorbar_label='',\n",
    "             axislabels=('', ''))\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "\n",
    "  ax = fig.add_subplot(223)\n",
    "  display.plot('reflectivity', 1, ax=ax,\n",
    "             title='Reflectivity second', colorbar_label='')\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "\n",
    "  ax = fig.add_subplot(224)\n",
    "  display.plot('cross_correlation_ratio', 0, ax=ax,\n",
    "             title='Correlation Coefficient', colorbar_label='',\n",
    "             axislabels=('East West distance from radar (km)', ''))\n",
    "  display.set_limits((-300, 300), (-300, 300), ax=ax)\n",
    "\n",
    "  fig.savefig(outpng)\n",
    "  \n",
    "if __name__ == '__main__':\n",
    "  import argparse\n",
    "  parser = argparse.ArgumentParser(description='plot some radar data')\n",
    "  parser.add_argument('nexrad', help=\"volume scan filename\")\n",
    "  parser.add_argument('png', help=\"output png filename\")\n",
    "  args = parser.parse_args()\n",
    "\n",
    "  print \"Plotting {} into {}\".format(args.nexrad, args.png)\n",
    "  plot_data(args.nexrad, args.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%bash\n",
    "python plot_pngs.py data/KIWA20130723_235451_V06.gz radarplot.png"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Create animating PNG </h2>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%bash\n",
    "rm -rf images\n",
    "mkdir images\n",
    "for volumefile in $(ls data); do\n",
    "    base=$(basename $volumefile)\n",
    "    python plot_pngs.py data/$volumefile images/$base.png\n",
    "done"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
